package cn.wolfcode.crm.config;

import cn.wolfcode.crm.shiro.CrmRealm;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 *
 * shiro配置
 */
@Configuration
public class ShiroConfig {
    @Autowired
    private CrmRealm crmRealm;

    /**
     * 配置安全管理器，注入数据源  校验
     * @return
     */
    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager(){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(crmRealm);
        return defaultWebSecurityManager;
    }
    //配置shiro拦截器
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager());
        shiroFilterFactoryBean.setLoginUrl("/login.html");
        shiroFilterFactoryBean.setFilterChainDefinitions("/loginUser.do = anon\n" +
                "                /logout.do = logout\n" +
                "                /js/** = anon\n" +
                "                /css/** = anon\n" +
                "                /** = authc");
        return shiroFilterFactoryBean;
    }

    /**
     * shiro注解扫描器
     * @return
     */
    @Bean
    public AuthorizationAttributeSourceAdvisor advisor(){
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(defaultWebSecurityManager());
        return advisor;
    }

}
